﻿using System;
using System.IO;
using Com.AAS.Common.Utils;

namespace Com.AAS.Toolbox.DataMirror.Server.Handler.Impl
{
    class FileSystemImageHandlerImpl : BaseHandlerImpl, FileSystemImageHandler
    {
        #region FileSystemImageHandler 成员
                
        public string MonitorPath       {          get;           set;       }

        public string[] ImagePaths { get ; set ; }


        public void OnProcess(MonitorEvent e)
        {
            EntityData data = e.EntityData;
            foreach (String imagePath in ImagePaths)
            {
                try
                {
                    // 删除操作
                    if (e.EventType == EventType.DATA_SYNC_DEL)
                    {
                        if (data.EntityType == EntityType.DIRECTORY)
                        {
                            DirectoryInfo newDirectoryInfo = new DirectoryInfo(imagePath + "\\" + data.OriginalName);
                            // 目录暂时不需要备份
                            if (newDirectoryInfo.Exists)
                                newDirectoryInfo.Delete();
                        }
                        else
                        {
                            FileInfo newFileInfo = new FileInfo(imagePath + "\\" + data.OriginalName);
                            DeleteFile(newFileInfo, new DirectoryInfo(imagePath));
                        }
                    }
                    // 重命名操作
                    else if (e.EventType == EventType.DATA_SYNC_REN)
                    {
                        if (data.EntityType == EntityType.DIRECTORY)
                        {
                            DirectoryInfo newDirectoryInfo = new DirectoryInfo(imagePath + "\\" + data.OriginalName);
                            DirectoryInfo oldDirectoryInfo = new DirectoryInfo(imagePath + "\\" + data.OldOriginalName);
                            if (oldDirectoryInfo.Exists)
                            {
                                Directory.Move(oldDirectoryInfo.FullName, newDirectoryInfo.FullName);
                            }
                        }
                        else
                        {
                            FileInfo newFileInfo = new FileInfo(imagePath + "\\" + data.OriginalName);
                            FileInfo oldFileInfo = new FileInfo(imagePath + "\\" + data.OldOriginalName);
                            if (oldFileInfo.Exists)
                            {
                                oldFileInfo.MoveTo(newFileInfo.FullName);
                            }
                        }
                    }
                    else
                    {
                        if (data.EntityType == EntityType.DIRECTORY)
                        {
                            DirectoryInfo newDirInfo = new DirectoryInfo(imagePath + "\\" + data.OriginalName);
                            if (!newDirInfo.Exists)
                                newDirInfo.Create();
                        }
                        else
                        {
                            FileInfo newFileInfo = new FileInfo(imagePath + "\\" + data.OriginalName);

                            // 检查目录是否存在,不存在则创建
                            if (!newFileInfo.Directory.Exists)
                                newFileInfo.Directory.Create();

                            // 检查文件是否存在
                            if (newFileInfo.Exists) 
                            {
                                String md5 = MD5Util.MD5(newFileInfo.FullName);
                                if (!md5.Equals(data.OriginalMD5Code) || md5.Length != 32)
                                {
                                   
                                    newFileInfo.Delete();
                                    File.Copy(data.OriginalFullPath, imagePath + "\\" + data.OriginalName);
                                }
                            }
                            else{
                                // 不存在则复制一份新的文件
                                File.Copy(data.OriginalFullPath, imagePath + "\\" + data.OriginalName);
                            }
                        }
                    }
                }
                catch (Exception e1)
                {
                    Console.WriteLine(e1);
                }
            }
        }

        #endregion

        private void DeleteFile(FileInfo info, DirectoryInfo parentDir)
        {
            if (!info.Exists)
                return;

            DirectoryInfo backupDirInfo = new DirectoryInfo(parentDir.FullName + "\\.cache");
            if (!backupDirInfo.Exists)
                backupDirInfo.Create();

            FileInfo newFileFullName = new FileInfo(backupDirInfo.FullName + info.FullName.Replace(parentDir.FullName, ""));
            int index = 1;
            while (newFileFullName.Exists)
            {
                String name = newFileFullName.Name;
                String nameTag1 = name;
                String nameTag2 = "";
                int lastIndex = name.LastIndexOf('.');
                if (lastIndex < name.Length - 1)
                {
                    nameTag1 = name.Substring(0, lastIndex);
                    nameTag2 = name.Substring(lastIndex + 1, name.Length - lastIndex - 1);
                }
                newFileFullName = new FileInfo(newFileFullName.DirectoryName + "\\" + nameTag1 + "(" + index + ")" + nameTag2);
            }

            if (!newFileFullName.Directory.Exists)
                newFileFullName.Directory.Create();

            info.MoveTo(newFileFullName.FullName);
        }

    }
}
